package myqueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yi
 * Date: 2024-07-19
 * Time: 20:47
 */
public class MyCircularQueue {
    private int[] elem;
    private static final int DEFAULT_CAPACITY = 10;
    private int front;
    private int rear;

    public MyCircularQueue(){
        elem = new int[DEFAULT_CAPACITY];
    }

    public MyCircularQueue(int capacity){
        this.elem = new int[capacity];
    }

    public boolean isEmpty(){
        return front == rear;
    }

    public boolean isFull(){
        return ((rear+1)%this.elem.length+1)%this.elem.length == front;
    }

    public boolean enQueue(int val){
        if(this.isFull()){
            return false;
        }
        this.elem[this.rear] = val;
        rear = (rear+1)%this.elem.length;
        return true;
    }

    public int Front(){
        if(isEmpty()){
            return -1;
        }
        int tmp = this.elem[this.front];
        front = (front+1)%this.elem.length;
        return tmp;
    }

    public int Rear(){
        if(isEmpty()){
            return -1;
        }
        return (rear == 0) ? this.elem[this.elem.length - 1] : this.elem[rear - 1];
    }
}
